/**
 * 系统字典管理设置
 * 
 * @version 1.0.0
 * @author yangyibao
 */
var dictDataTable;

$(function () {
	var aoColumns = [{"mDataProp":"mdl"},{"mDataProp":"name"},{"mDataProp":"dtype"},{"mDataProp":"val"},{"mDataProp":"model"},{"mDataProp":"remark"},{"mDataProp":"status"},{"mDataProp":"mdl"}]
	var aoColumnDefs = [
	                    {"aTargets":[2],"mRender":function(data,type,full){if(data=="String") return "字符型"; else if(data=="int") return "整数型"; else if(data=="password") return "密码型";  else return "浮点型"}},
	                    {"aTargets":[3],"mRender":function(data,type,full){if(full.dtype=="password") return "*****"; else return data}},
	                    {"aTargets":[7],"mRender":function(data,type,full){
		                      return "<button class='btn btn-lightblue btn-outline m-wrap' style='line-height:10px;font-size:10px;' data-json='"+JSON.stringify(full)+"' id='uptBtn'><i class=' icon-pencil'></i></button>" +"&nbsp;&nbsp;"+
		                      		 "<button class='btn btn-lightpink btn-outline m-wrap' style='line-height:10px;font-size:10px;'  data-mdl='"+full.mdl+"' data-name='"+full.name+"' id='dltBtn'><i class='icon-remove'></i></button>";}},
                        {"aTargets":[6],"mRender":function(data){return (data==1)?"<span class='font-green-jungle'>开启</spn>":"<span class='font-red-thunderbird'>关闭</span>";}}];
	dictDataTable = showDataTable($('#dictDataTable'),"../system/dict/queryDicts",aoColumns,aoColumnDefs);
		
	//删除字典信息
	$('#dictDataTable tbody').on('click','#dltBtn',function(){
		var mdl = $(this).attr("data-mdl");
		var name = $(this).attr("data-name");
		confirm("删除模块["+mdl+"->"+name+"]?",function(isConfirm){if (isConfirm) {$.post("../system/dict/deleteDict",{mdl:mdl,name:name},function(data){if(data){dictDataTable.ajax.reload(null,false);succ();}else error();});} else  cancel();});
	});
	
	//更新字典信息
	$('#dictDataTable tbody').on('click','#uptBtn',function(){
		changValType();
		$("#dictAddInfoForm #mdl").attr("disabled","disabled");
		$("#dictAddInfoForm #name").attr("disabled","disabled");
		$("#dictAddInfoForm .error span").remove();
		$("#dictAddInfoForm .error").removeClass("error");
		$("#dictAddInfoForm .success span").remove();
		$("#dictAddInfoForm .success").removeClass("success");
		jsonVal($.parseJSON($(this).attr("data-json")));
		$("#dictAddInfoDiv").modal("show");
		validatezs($("#dictAddInfoForm"), {mdl:{ required: true, maxlength:25},
    	     name:{ required:true, maxlength:25},
    	     val:{ required:true, maxlength:50 },
    	     remark:{ required:true, maxlength:30 }},
    	     '../system/dict/updateDict',dictDataTable,$("#dictAddInfoDiv"));
	});
	
	//新增字典表信息
	$("#dictAddDivBtn").click(function() {
		$("#dtype option[value='String']").attr("selected",true);
		$("#dictAddInfoForm #mdl").removeAttr("disabled");
		$("#dictAddInfoForm #name").removeAttr("disabled");
		$("#dictAddInfoForm .error span").remove();
		$("#dictAddInfoForm .error").removeClass("error");
		$("#dictAddInfoForm .success span").remove();
		$("#dictAddInfoForm .success").removeClass("success");
		$("#dictAddInfoDiv").modal("show");
		validatezs($("#dictAddInfoForm"), {mdl:{ required: true, maxlength:25 },
			name:{ required:true, maxlength:25 },
			val:{ required:true, maxlength:50 },
			remark:{ required:true, maxlength:30 }}, 
			'../system/dict/saveDict',dictDataTable,$("#dictAddInfoDiv"));
		$("#dictAddInfoForm")[0].reset();
	});
	
	//保存按钮处理
	$("#dictAddBtn").click(function(){
		$("#dictAddInfoForm #mdl").removeAttr("disabled");
		$("#dictAddInfoForm #name").removeAttr("disabled");
		$("#dictAddInfoForm").submit();
		
 	});
	function changValType(){
		var val = $("#dictAddInfoForm #dtype").val();
		if(val == "password"){
			$("#dictAddInfoForm #val").attr("type","password");
			$("#dictAddInfoForm #val").val("");
		}else{
			$("#dictAddInfoForm #val").attr("type","text");
		}
	}
	
	//修改密码处理
	$("#dtype").change(function(){
		changValType();
	})
	//更新赋值
	function jsonVal(json){
		$("#mdl").val(json.mdl);
		$("#name").val(json.name);
		$("#dtype option[value='"+json.dtype+"']").attr("selected",true);
		$("#val").val(json.val);
		changValType();
		$("#remark").val(json.remark);
		$("#model option[value='"+json.model+"']").attr("selected",true);
		$("input[name='status'][value='"+json.status+"']").prop("checked",true);
		$("input[name='status'][value='"+json.status+"']").click();
		
		
	}
});


//重新加载字典数据
function reloadDictData() {
	dictDataTable.ajax.reload();
}
//刷新
function reloadDict() {
	dictDataTable.ajax.reload(null,false);
}